#include "Items.h"
#include "Global References/Globals.h"
#include <math.h>
#include "World/Entity/Local References.h"
#include "World/Entity/Entity.h"
Item_Projectile::Item_Projectile():Item_Base()
{
	vector		= Vector2D(0,0);
	m_v3Pos.x	= 0;
	m_v3Pos.y	= 0;
	m_v3Pos.z	= 0;
	physicsOn	= true;
	movementOn	= true;
}
void Item_Projectile::_projectileInit(bool movement, bool physics, Vector3D PIE, Vector2D a_vector, short a_range, short ownerFlag)
{
	movementOn	= movement;
	range		= a_range;
	physicsOn	= physics;
	pieDmg		= PIE;
	vector		= a_vector;
	originFlag	= ownerFlag;



	float percentHorizontal = 100;
	
	
	//Determine the angle at which the projectile will be launched from the origin
	percentHorizontal -= (float)(((int)(a_vector.y) % 90) * 1.1f);
	
	if((a_vector.y >= 0 && a_vector.y <= 90) || (a_vector.y > 180 && a_vector.y <= 270))
	{
		vector.x =  (float)(a_vector.x * (percentHorizontal*.01));
		vector.y =  (float)(a_vector.x * ((100-percentHorizontal)*.01));
	}
	if((a_vector.y > 90 && a_vector.y <= 180) || (a_vector.y > 270 && a_vector.y <= 360))
	{
		vector.y =  (float)(a_vector.x * (percentHorizontal*.01));
		vector.x =  (float)(a_vector.x * ((100-percentHorizontal)*.01));
	}

	if(a_vector.y >= 90 && a_vector.y <= 270) 	vector.x *= -1;
	if(a_vector.y >= 180 && a_vector.y <= 360)	vector.y *= -1;

}


void Item_Projectile::update()
{
	if(movementOn)
	{
		if(physicsOn)
			vector.y += Gravity;

		m_v3Pos.x += vector.x;
		m_v3Pos.y += vector.y;

		range -= (short)(fabs(vector.x));
		range -= (short)(fabs(vector.y));
	}
}

void Item_Projectile::serialize(ByteBuffer* info)
{
	// serialize everything by pushing each variable into a buffer
	Item_Base::serialize(info);
	info->addShort(range);
	info->addFloat(vector.x);
	info->addFloat(vector.y);
	info->addShort(originFlag);
	info->addShort(collideEffect);
	info->addShort(movementOn);
	info->addShort(physicsOn);
}

void Item_Projectile::deserialize(ByteBuffer* info, int & index)
{
	// deserialize by taking information from the buffer
	short shortSize = sizeof(short);
	short floatSize = sizeof(short) + sizeof(long);

	Item_Base::deserialize(info, index);
	range = info->getShort(index); index += shortSize;
	vector.x = info->getFloat(index); index += floatSize;
	vector.y = info->getFloat(index); index += floatSize;
	originFlag = info->getShort(index); index += shortSize;
	collideEffect = info->getShort(index); index += shortSize;
	if(info->getShort(index))
		movementOn = true;
	else
		movementOn = false;
	index += shortSize;
	
	if(info->getShort(index))
		physicsOn = true;
	else
		physicsOn = false;
	index += shortSize;
}

